<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
      <FormPanel ref="formPanel" :list="formList" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
       <BaseDialog :visible.sync="visible" :title="'配件选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <Vehicle @close="close" @save="save"></Vehicle>
      </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import { selectById, editSave } from '@/api/claim/factory/PartRecoveryCoefficient';
import { getPartType } from '@/api/dictionary/index';
import Vehicle from '@/pages/claimDemage/tools/vehicle.vue';
export default {
 components: {
    Vehicle
  },
  props: ['type', 'rowData'],
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList1(),
      saveData: null,
      activeName: '审核记录',
      newVar: {}
    };
  },
  created() {
   // this.chuqi()
  },
  mounted() {
    this.init();
  },
  methods: {
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    async init() {
      this.loading = true;
      let newVar = await selectById({id: this.rowData.ppId});
      this.formList.forEach(x => {
        console.log(this.rowData);
        x.initialValue = newVar.data[x.fieldName];
      });
      this.newVar = newVar.data;
      this.loading = false;
    },
    async chuqi() {
      const res = await getPartType({type: 5586});
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'type').initialValue = '';
        this.formList.find(x => x.fieldName == 'type').itemList = res.data.map(y => ({
          text: y.codeCnDesc,
          value: y.codeId
        }));
      }
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        console.log(this.saveData);
        //this.saveData.para = this.rowData.para;
        this.saveData.ppId = this.rowData.ppId;
        this.saveData.isDeleted = this.rowData.isDeleted;
        this.saveData.updatedAt = null;
        this.saveData.updatedBy = null;
        for (let key in this.saveData) {
          this.newVar[key] = this.saveData[key];
        }
        this.loading = true;
        let res = await editSave(this.newVar);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close');
    },
    add() {},
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      console.log(obj);
      this.formList.find(x => x.fieldName == 'partCode').initialValue = obj[0].partCode;
      this.formList.find(x => x.fieldName == 'partName').initialValue = obj[0].partName;
      this.formList.find(x => x.fieldName == 'partUnit').initialValue = obj[0].partUnit;
      this.formList.find(x => x.fieldName == 'para').initialValue = obj[0].para;
      this.visible = false;
    },
    close() {
      this.visible = false;
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partCode',
          placeholder: '请选择配件代码...',
          readonly: true,// 只读
          rules: [{ required: true, message: '请选择配件代码...', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()}/>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          readonly: true,// 只读
          disabled: true,// 置会
          // rules: [{ required: true, message: '请输入配件名称', trigger: 'blur' }, { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
          rules: [{ required: true, message: '请输入配件名称', trigger: 'blur' }],
          placeholder: '请输入配件名称...'
        },
        {
          type: 'INPUT',
          label: '单位',
          readonly: true,// 只读
          disabled: true,// 置会
          fieldName: 'partUnit',
          placeholder: '请输入配件名称...',
          rules: [{ required: true, message: '请输入配件名称...', trigger: 'blur' }],
          offsetRightCols: 1
        },
       {
          type: 'INPUT',
          label: '追偿系数',
          readonly: false,// 只读
          disabled: false,// 置会
          fieldName: 'para',
          placeholder: '请输入追偿系数...',
          rules: [{ required: true, message: '请输入追偿系数...', trigger: 'blur' }],
          offsetRightCols: 1
        }
        // {
        //   type: 'SELECT',
        //   label: '配件类型',
        //   readonly: true,// 只读
        //   fieldName: 'type',
        //   placeholder: '请选择配件类型...',
        //   rules: [{ required: true, message: '请选择配件类型', trigger: 'blur' }],
        //   selfCols: 3,
        //   offsetRightCols: 1
        // },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
